<?php
/**
 * Created by PhpStorm
 * user: MingLiang
 * Date：2021/8/20
 * Time: 2:32 下午
 */


namespace app\admin\model;


use app\common\constants\MenuConstant;
use app\common\model\TimeModel;

use function Complex\sec;

class FinalAccountsClassify extends TimeModel
{
    protected $table = "";
    protected $deleteTime = 'delete_time';

    public function company()
    {
        return $this->belongsTo('app\admin\model\CostCompany', 'company_id', 'id');
    }

    public function getPidMenuList()
    {
        $list        = $this->field('id,pid,title')
            ->where([
                ['pid', '<>', MenuConstant::HOME_PID],
            ])
            ->select()
            ->toArray();
        $pidMenuList = $this->buildPidMenu(0, $list);
        $pidMenuList = array_merge([[
            'id'    => 0,
            'pid'   => 0,
            'title' => '顶级分类',
        ]], $pidMenuList);
        return $pidMenuList;
    }

    protected function buildPidMenu($pid, $list, $level = 0)
    {
        $newList = [];
        foreach ($list as $vo) {
            if ($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                if ($level > 1) {
                    $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $markString   = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
                    $vo['title']  = $markString . $vo['title'];
                }
                $newList[] = $vo;
                $childList = $this->buildPidMenu($vo['id'], $list, $level);
                !empty($childList) && $newList = array_merge($newList, $childList);
            }

        }
        return $newList;
    }

    public static function getClass()
    {
        $list = self::field('id,pid')
            ->where([
                ['pid', '=', 0],
            ])
            ->select()
            ->toArray();
        if($list){
            foreach($list as $value){
                $arr[$value['id']] = self::PidList($value['id']);
            }
            foreach ($arr as $key => $value){
                $in[$key] = array_merge([$key],$value);
            }
            return $in;
        }
        return false;
    }

    public static function PidList($id){
        $list = [];
        $data = self::field('id,pid')->where('pid',$id)->select()->toArray();
        if($data){
            //15,16,19
            foreach ($data as $value){
                $childList = self::PidList($value['id']);
                $list[] = $value['id'];
                !empty($childList) && $list = array_merge($list, $childList);
            }
        }
        return $list;


    }


    


}